package logic

import (
	"book_srv/book_srv"
	"book_srv/internal/svc"
	"book_srv/models"
	"context"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type PushBookLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPushBookLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PushBookLogic {
	return &PushBookLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *PushBookLogic) PushBook(in *book_srv.PushBookRequest) (*book_srv.PushBookResponse, error) {
	// todo: add your logic here and delete this line
	key := fmt.Sprintf("%d:%s", in.AuthorId, in.Title)
	book := models.Book{
		Title:    in.Title,
		AuthorId: in.AuthorId,
		Image:    in.Image,
	}
	if models.DB.Create(&book).Error != nil {
		return nil, fmt.Errorf("服务器繁忙")
	}
	models.RDB.Set(models.Ctx, key, book, 0)
	return &book_srv.PushBookResponse{}, nil
}
